package com.powernode.teatraceability.service.contract.impl;


import com.powernode.teatraceability.constant.CustomerConstant;
import com.powernode.teatraceability.raw.Enterprise;
import com.powernode.teatraceability.raw.Product;
import com.powernode.teatraceability.service.contract.ProductCertificateService;
import com.powernode.teatraceability.vo.ProductCertificateVo;
import lombok.extern.slf4j.Slf4j;
import org.fisco.bcos.sdk.abi.datatypes.generated.tuples.generated.Tuple7;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.model.TransactionReceipt;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;

/**
 * ClassName: ProductCertificateImpl
 * Description:
 *
 * @Author: jdz
 * @Create: 2023/5/3 - 12:02
 */
@Service
@Slf4j
public class ProductCertificateImpl implements ProductCertificateService {
    @Value("${system.contract.productAddress}")
    private String contractAddress;
    @Resource
    private Client client;

    @Override
    public boolean applyProductCertificate(String _productAddress, String _name, String _certificateType, String _certificateMaterial, String _enterpriseName) {
        final Product product = Product.load(contractAddress, client, client.getCryptoSuite().createKeyPair());
        final TransactionReceipt transactionReceipt = product.applyProductCertificate(_productAddress, _name, _certificateType, _certificateMaterial, _enterpriseName);
        log.info("applyProductCertificate traction receipt {}", transactionReceipt.toString());
        log.error(transactionReceipt.getMessage());
        final String status = transactionReceipt.getStatus();
        return CustomerConstant.SUCCESS_STATUS.equals(status);
    }

    @Override
    public String approveProductCertificate(String _address, BigInteger _index) {
        final Product product = Product.load(contractAddress, client, client.getCryptoSuite().createKeyPair());
        final TransactionReceipt transactionReceipt = product.approveProductCertificate(_address, _index);
        log.info("approveProductCertificate traction receipt {}", transactionReceipt.toString());
        final String status = transactionReceipt.getStatus();
        if (CustomerConstant.SUCCESS_STATUS.equals(status)) {
            return transactionReceipt.getTransactionHash();
        }
        return null;
    }

    @Override
    public boolean changeProductCertificate(String _address, BigInteger _index, String _name, String _certificateType, String _certificateMaterial, String _enterpriseName) {
        final Product product = Product.load(contractAddress, client, client.getCryptoSuite().createKeyPair());
        final TransactionReceipt transactionReceipt = product.changeProductCertificate(_address, _index, _name, _certificateType, _certificateMaterial, _enterpriseName);
        log.info("changeProductCertificate traction receipt {}", transactionReceipt.toString());
        final String status = transactionReceipt.getStatus();
        return CustomerConstant.SUCCESS_STATUS.equals(status);
    }

    @Override
    public ProductCertificateVo getProductCertificate(String _address, BigInteger _index) {
        final Product product = Product.load(contractAddress, client, client.getCryptoSuite().createKeyPair());
        try {
            final Tuple7<String, String, String, String, BigInteger, Boolean, String> productCertificate = product.getProductCertificate(_address, _index);
            if (productCertificate.getValue1().startsWith("0x00000000000000")) {
                return null;
            } else {
                return new ProductCertificateVo(
                        productCertificate.getValue1(),
                        productCertificate.getValue2(),
                        productCertificate.getValue3(),
                        productCertificate.getValue4(),
                        productCertificate.getValue5(),
                        productCertificate.getValue6(),
                        productCertificate.getValue7());
            }
        } catch (ContractException e) {
            e.printStackTrace();
            return null;
        }
    }
}
